﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace XMLToCS
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private XmlDocument m_Xml = new XmlDocument();
        private string m_CSStr = "";
        private void PrintfToCS(XmlNodeList nodelist, string parent)
        {

            foreach (XmlNode node in nodelist)
            {
                if (node.ChildNodes?.Count > 1)
                {
                    m_CSStr += @"public class " + "" + parent + "_" + node.Name + @"
{
";
                    PrintfToCS(node.ChildNodes, parent + "_" + node.Name);

                    m_CSStr += @"
}
";
                    m_CSStr += @"public " + parent + "_" + node.Name + " _" + parent + "_" + node.Name + " = new " + parent + "_" + node.Name + "(); " + @"
";
                }
                else if (node.ChildNodes?.Count == 1)
                {
                    m_CSStr += @"public class " + "" + parent + "_" + node.Name + @"
{";
                    m_CSStr += @"
                                    public string m_Value = " + "\"XXX\"" + @";
";
                    m_CSStr += @"
}
";
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                m_Xml.Load(openFileDialog1.FileName);
                var xmlNodeList = m_Xml.ChildNodes;
                PrintfToCS(xmlNodeList[1].ChildNodes, "");
                using (FileStream fs = new FileStream(openFileDialog1.FileName + ".cs", FileMode.Create))
                {
                    fs.Flush();
                    var buff = Encoding.UTF8.GetBytes(m_CSStr);
                    fs.Write(buff, 0, buff.Length);
                }
            }


            ////清空缓冲区、关闭流
            //fs.Flush();

            //fs.Close();
            //fs.Dispose();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string str;
            XMLSample xMLSample = new XMLSample();
            var xmlserializer = new XmlSerializer(typeof(XMLSample));
            using (StringWriter stringWriter = new StringWriter())
            {
                using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true }))
                {
                    xmlserializer.Serialize(writer, xMLSample);
                    str = stringWriter.ToString();
                }
            }
            using (FileStream fs = new FileStream(openFileDialog1.FileName + ".two", FileMode.Create))
            {
                fs.Flush();
                var buff = Encoding.UTF8.GetBytes(str);
                fs.Write(buff, 0, buff.Length);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var text = new XMLtext();
            text.m_M.Value = "一百块";
            text.m_P.Value = "13人";
            string remark = text.GetRemark();


            string totail = remark+ ":\r\n" + text.m_M.GetRemark() + "=" + text.m_M.Value;
            totail +="\r\n" +text.m_P.GetRemark() + "=" + text.m_P.Value;
            //Exhibition(text);
        }

        void Exhibition(XMLtext sample)
        {
            System.Reflection.MemberInfo info = typeof(XMLtext);
            //info.GetCustomAttribute
            object[] attributes = info.GetCustomAttributes(typeof(MyExtendAttribute), true);

            var propertiesinfo = sample.GetType().GetFields();
            foreach (var field in propertiesinfo)
            {
                if (field.IsDefined(typeof(MyExtendAttribute), true))
                {
                    //var attribute = (Attribute)field.GetCustomAttributes
                    //if(attribute.)
                }
            }


            foreach (var item in attributes)
            {
                string remark;
                if (item.GetType().Name == "MyExtendAttribute")
                {
                    remark = item.GetType().Name;
                }



            }
        }
    }

    [MyExtend("集体")]
    class XMLtext
    {
        [MyExtend("人数")]
        public class People
        {
            public string Value;
        }

        public People m_P = new People();
        [MyExtend("钱数")]
        public class Money
        {
            public string Value;
        }
        public Money m_M = new Money();
    }
    static class ExTendTool
    {
        /// <summary>
        /// 字符串转Unicode码
        /// </summary>
        /// <returns>The to unicode.</returns>
        /// <param name="value">Value.</param>
        public static string ToU(this string value)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(value);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                // 取两个字符，每个字符都是右对齐。
                stringBuilder.AppendFormat("u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// Unicode转字符串
        /// </summary>
        /// <returns>The to string.</returns>
        /// <param name="unicode">Unicode.</param>
        public static string ToZ(this string unicode)
        {
            string resultStr = "";
            string[] strList = unicode.Split('u');
            for (int i = 1; i < strList.Length; i++)
            {
                resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
            }
            return resultStr;
        }

        public static string GetRemark(this object xmlcontent)
        {
            Type t =xmlcontent.GetType();


            //var attributelist = t.GetCustomAttributes(true);

            if (t.IsDefined(typeof(XMLToCS.MyExtendAttribute), true))
            {
                //var fieldinfo = t.GetField( "XMLToCS.MyExtendAttribute");
                var listdata = t.GetCustomAttributesData();
                foreach (CustomAttributeData item in listdata)
                {
                    if (item.ToString().IndexOf("XMLToCS.MyExtendAttribute") != -1)
                        return item.ConstructorArguments[0].Value as string; //构造函数 参数第一个 的值..作为string 返回;
                    //some.Name;
                }
            }

            //var s = xmlcontent.ToString();
            //FieldInfo field = t.GetField(xmlcontent.ToString());
            //try
            //{
            //    if (field.IsDefined(typeof(MyExtendAttribute), true))
            //    {
            //        //MyExtendAttribute attribute = field.get(typeof(MyExtendAttribute))
            //        MyExtendAttribute remarkAttribute = field.GetCustomAttributes(typeof(MyExtendAttribute), true)[0] as MyExtendAttribute;
            //        return remarkAttribute.Remark;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}




            //RemarkAttribute remarkAttribute = (RemarkAttribute)field.GetCustomAttribute(typeof(RemarkAttribute));
            //return remarkAttribute.Remark;
            return "无";
        }
    }
}
